package controller;

import java.util.Scanner;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import service.StoreAndAdminManager;
import service.StoreManager;

import com.foodshop.admin.jms.AdminJMS;

@Controller
public class StroeAndAdminController {
	
	private long UUID ;
	
	@Autowired(required = true)
	private StoreManager sm;
	
	@Autowired(required = true)
	private StoreAndAdminManager sam;
	
	@Autowired(required = true)
	private AdminJMS adminJMS;
	
	@RequestMapping(value="/showStoreInfo")
	public String showStoreInfo(HttpServletResponse response) {
		
		
		return sam.findStoreInfo(response,adminJMS)? "success":"fail";

	}
	
	public static void main(String[] args) {

		Thread thread = new Thread() {

			@Override
			public void run() {
				while (true) {
					
					String path = "tcp://115.28.88.171:61616";//蒋垚
					ConnectionFactory factory = new ActiveMQConnectionFactory(path);
					
						Connection conn;
						try {
							conn = factory.createConnection();
							Destination topic = new ActiveMQTopic("adminTopic");
							
							conn.start();
							Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
							MessageConsumer consumer = session.createConsumer(topic);
							TextMessage message =(TextMessage)consumer.receive();
							String msg = message.getText();
						if (msg.equals("0")) {
							session.createProducer(new ActiveMQTopic("storeTopic")).send(session.createTextMessage("http://192.168.3.133:8080/Store/showStoreInfo"));
							
						}
						consumer.close();
						session.close();
						conn.close();
						} catch (JMSException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					
				}
			}
			
		};
		thread.start();
		new Scanner(System.in).nextLine();
	}
}
